package com.xuecheng.manage_cms.controller;

import com.xuecheng.api.cms.CmsPageControllerApi;
import com.xuecheng.framework.domain.cms.CmsPage;
import com.xuecheng.framework.domain.cms.request.QueryPageRequest;
import com.xuecheng.framework.domain.cms.response.CmsPageResult;
import com.xuecheng.framework.domain.cms.response.CmsPostPageResult;
import com.xuecheng.framework.domain.cms.response.QueryResponseResult;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.framework.web.BaseController;
import com.xuecheng.manage_cms.service.PageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import java.io.IOException;

/**
 * @author Administrator
 * @version 1.0
 * @create 2018-09-12 17:24
 **/
@RestController
@RequestMapping("/cms")
@Api(tags = "cms页面管理接口",description = "cms页面管理接口，提供页面的增、删、改、查")
public class CmsPageController extends BaseController implements CmsPageControllerApi {

    @Autowired
    PageService pageService;

    @Override
    @GetMapping("/page/list/{page}/{size}")
    @ApiOperation("分页查询页面列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true,paramType = "path",dataType = "int"),
            @ApiImplicitParam(name = "size",value = "每页记录数",required = true,paramType = "path",dataType = "int")
    })
    public QueryResponseResult findList(@PathVariable("page") int page, @PathVariable("size")int size,QueryPageRequest queryPageRequest) {
        //调用service
        return pageService.findList(page,size,queryPageRequest);
    }

    @Override
    @GetMapping("/page/site/list")
    @ApiOperation("查询站点列表")
    public QueryResponseResult findSiteList() {
        return pageService.findSiteList();
    }

    @Override
    @GetMapping("/page/template/list")
    @ApiOperation("查询模板列表")
    public QueryResponseResult findTemplateList() {
        return pageService.findTemplateList();
    }

    @Override
    @PostMapping("/page/add")
    @ApiOperation("添加页面")
    public CmsPageResult add(@RequestBody CmsPage cmsPage) {
        return pageService.add(cmsPage);
    }

    @Override
    @GetMapping("/page/get/{id}")
    @ApiOperation("根据页面id查询页面")
    public CmsPage queryCmsPageById(@PathVariable("id") String pageId) {
        return pageService.queryCmsPageById(pageId);
    }

    @Override
    @PutMapping("/page/update")
    @ApiOperation("更新页面")
    public CmsPageResult updatePage(@RequestBody CmsPage cmsPage) {
        return pageService.upate(cmsPage);
    }

    @Override
    @DeleteMapping("/page/delete/{id}")
    @ApiOperation("删除页面")
    public ResponseResult deletePage(@PathVariable("id") String pageId) {
        return pageService.deletePage(pageId);
    }

    @Override
    @ApiOperation("发布页面")
    @PostMapping("/page/postPage/{pageId}")
    public ResponseResult postPage(@PathVariable("pageId") String pageId) {
        return pageService.postPage(pageId);
    }

    @Override
    @ApiOperation("保存页面，存在时更新，没有时新增")
    @PostMapping("/page/save")
    public CmsPageResult save(@RequestBody CmsPage cmsPage) {
        return pageService.save(cmsPage);
    }

    @ApiOperation("页面预览")
    @RequestMapping(value = "/preview/{pageId}",method = RequestMethod.GET)
    public void preview(@PathVariable("pageId")String pageId){
        String pageHtml = pageService.getPageHtml(pageId);
        if (StringUtils.isNotBlank(pageHtml)){
            try {
                ServletOutputStream outputStream = response.getOutputStream();
                response.setHeader("Content-type","text/html;charset=utf-8");
                outputStream.write(pageHtml.getBytes("utf-8"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    @ApiOperation("一键发布页面")
    @PostMapping("/page/postPageQuick")
    public CmsPostPageResult postPageQuick(@RequestBody CmsPage cmsPage) {
        return pageService.postPageQuick(cmsPage);
    }
}
